package com.ruoyi.web.web.controller;

import com.ruoyi.web.core.config.AppConf;
import com.ruoyi.accountant.domain.Account;
import com.ruoyi.accountant.domain.Reason;
import com.ruoyi.accountant.domain.TransactionDetail;
import com.ruoyi.accountant.service.AccountService;
import com.ruoyi.accountant.service.ReasonService;
import com.ruoyi.accountant.service.TransactionDetailService;
import com.ruoyi.web.web.form.TransactionDetailListForm;
import com.ruoyi.web.web.form.Validation;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RequestMapping("/test")
@RestController
public class PageRestContraller {
    @Autowired
    private AccountService accountService;

    @Autowired
    private TransactionDetailService transactionDetailService;

    @Autowired
    private ReasonService reasonService;

    @RequestMapping(value = "/detail", method ={RequestMethod.GET, RequestMethod.POST} )
    public HashMap<Object, Object> transaction(TransactionDetailListForm transactionDetailListForm) {
        HashMap<Object, Object> map = new HashMap<Object, Object>();


        Validation validate = transactionDetailListForm.validate();
        if(validate.getOk()) {
            // 查询资金账户
            List<Account> accountList = accountService.findAll();
            map.put("accountList",accountList);
//            model.addAttribute("accountList", accountList);


            // 查询记录
            PageInfo<TransactionDetail> page = (PageInfo<TransactionDetail>) transactionDetailService.findAllByForm(transactionDetailListForm);
            int start = transactionDetailListForm.getPage() > 5 ? transactionDetailListForm.getPage() - 4 : 1;


            map.put("transactionDetailList",page);

            // 查询收支原因
            List<Reason> reasonList = reasonService.findAllByParent(AppConf.ROOT);
            map.put("accountList",accountList);
        }
        map.put("form",transactionDetailListForm);

        return map;
    }
}
